package vn.com.atm;


import org.json.JSONException;
import org.json.JSONObject;

import vn.com.atm.database.UserFunctions;
import vn.com.atm.library.Alert;
import vn.com.atm.library.SimpleCrypto;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class RegisterActivity extends Activity {
    Button btnRegister;
    TextView btnLinkToLogin;
    EditText inputFullName;
    EditText inputEmail;
    EditText inputPassword;
    TextView registerErrorMsg;
    private ProgressDialog pDialog;
    // JSON Response node names
    private static String KEY_SUCCESS = "status";
    public Alert alert;
    public String name, email, password;
    public int t = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
 
        // Importing all assets like buttons, text fields
        inputFullName = (EditText) findViewById(R.id.reg_fullname);
        inputEmail = (EditText) findViewById(R.id.reg_email);
        inputPassword = (EditText) findViewById(R.id.reg_password);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnLinkToLogin = (TextView) findViewById(R.id.link_to_login);
        registerErrorMsg = (TextView) findViewById(R.id.register_error);
 
        // Register Button Click event
       
        
        
        btnRegister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	  name = inputFullName.getText().toString();
                  email = inputEmail.getText().toString();
                  password = inputPassword.getText().toString();
                  
                  SimpleCrypto sp = new SimpleCrypto();
                  try {
                
                	  	password = sp.encrypt("key", password);
	      			} catch (Exception e1) {
	      				e1.printStackTrace();
	      			}
                  UserFunctions userFunction = new UserFunctions();
                  JSONObject json = userFunction.registerUser(name, email, password);

            	 if(TestInfo(name, password, email))
            	 {
            		
            		 alert = new Alert();      
            		 try {
                     	
                         if (json.getString(KEY_SUCCESS) != null) {
                            // registerErrorMsg.setText("");
                             String res = json.getString(KEY_SUCCESS);
                             if(res.equalsIgnoreCase("SUCCESS")== true){
                             	
                            	 alert.AlertError(RegisterActivity.this, "Thông tin", "Đăng kí thành công");
                    			 Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                                 startActivity(login);
                                 finish();
                             }else{
                                
                            	 alert.AlertError(RegisterActivity.this, "Lỗi", "Tài khoản đã tồn tại");
                             }
                         }
                     } catch (JSONException e) {
                         e.printStackTrace();
                     }
            	 }
            	
            }
            
            
        });
       
        // Link to Login Screen
        btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View view) {
            	
            	Intent i = new Intent(getApplicationContext(),LoginActivity.class);
            	startActivity(i);
            	
            }
        });
    }
    private boolean TestInfo(String pUserName, String pPass, String pEmail)
    {
    	alert = new Alert();
    	if(pUserName.equalsIgnoreCase("")||pPass.equalsIgnoreCase("")||pEmail.equalsIgnoreCase(""))
    	{
    		
    		alert.AlertError(RegisterActivity.this, "Lỗi", "Nhập thiếu thông tin");
    		return false;
    	}
    	else
    	{
    		if(!pEmail.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+"))
        	{
        		alert.AlertError(RegisterActivity.this, "Lỗi", "Nhập sai email");
        		return false;
        	}
    		return true;
    	}
    	
    	
    }
}